package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.dao.MenuMapper;

import com.itcast.dao.RoleMapper;
import com.itcast.dao.UserMapper;
import com.itcast.health.pojo.Menu;
import com.itcast.health.pojo.Role;
import com.itcast.health.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;




import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Set;



/**
 * @author Gao
 * @version v1.0
 * @date 2019/12/3 11:15
 * @description
 **/
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMpper;
    @Override
    //动态获取菜单
    public List<Menu> getMenuByusername(String username) {
        List<Menu> menuList=new ArrayList<>();
        //根据用户名调用userDao获取用户id
        User user = userMapper.findUserByname(username);
        Integer userId = user.getId();
        //根据用户用户id调用roleDao获取该用户对应的角色id
        Set<Role> roles = roleMpper.findRoleByUserid(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //获得一级菜单
            List<Menu> menu1=menuMapper.findMenu1ByRoleId(roleId);
            if (menu1!=null&&menu1.size()>0){
                for (Menu menu : menu1) {
                    Integer menuId = menu.getId();
                    //获得二级菜单
                    List<Menu> menu2 = menuMapper.findChildrenMuneById(menuId);
                    if (menu2!=null&&menu2.size()>0){
                        menu.setChildren(menu2);
                    }
                    menuList.add(menu);
                }
            }
        }
        return menuList;
    }
    /**
     * 获取所有菜单并返回前端页面
     *
     * @return
     */
    @Override
    public List<Menu> getMenu() {

        return menuMapper.getMenu();
    }


    /**
     * 根据id获取对应的菜单,菜单和角色的关系,子菜单
     *
     * @return
     */
    @Override
    public Map getMenuById(Integer id) {
       Menu menu= menuMapper.getMenuById(id);
       //获取所有id不是传入id和父id的菜单
       List<Map> menus= menuMapper.getChildMenuById(id,menu.getParentMenuId());
       //查询所有的父菜单集合
        List<Map> parentMenus=menuMapper.getParentMenu();
       //获取id对应子菜单的id集合
        Integer[] childIds=menuMapper.getChildIdsById(id);
        //获取id对应role-menu中role的id集合
        Integer[] roleIds=menuMapper.getRoleIdsById(id);
        //获取所有角色
        List<Map> roles=menuMapper.getRoles();
        Map map = new HashMap<>();
        map.put("menu",menu);
        map.put("menus",menus);
        map.put("parentMenu",parentMenus);
        map.put("childIds",childIds);
        map.put("roles",roles);
        map.put("roleIds",roleIds);
        return map;
    }

    /**
     * 根据id删除对应的菜单以及菜单和角色的关系
     *
     * @param id
     * @return
     */
    @Override
    public void delete(Integer id) {
        menuMapper.deleteRelationshipByMenuId(id);
        menuMapper.deleteMenuById(id);
    }


    /**
     * 编辑菜单所有数据
     *
     * @param childIds
     * @param roleIds
     * @param menu
     */
    @Override
    public void edit(Integer[] childIds, Integer[] roleIds, Menu menu) {
        //编辑菜单
        menuMapper.edit(menu);
        //先删除所有关系
        menuMapper.deleteRelationshipByMenuId(menu.getId());
        //添加role-menu关系
        List items = new ArrayList();
        if (roleIds.length>0) {
            for (Integer roleId : roleIds) {
                Map map = new HashMap();
                map.put("roleId", roleId);
                map.put("menuId", menu.getId());
                items.add(map);
            }
            menuMapper.addRelationship(items);
        }
        /**
         * 由于自关联 无法多对多
         *仅实现功能,逻辑有问题
         */
        if (childIds.length>0) {
            for (Integer childId : childIds) {
                menuMapper.editChild(childId, menu.getId());
            }
        }

    }
}

